<?php
if (!defined("PHPUnit_MAIN_METHOD")) {
    define("PHPUnit_MAIN_METHOD", "RssPublisherTest::main");
}

require_once "PHPUnit/Framework/TestCase.php";
require_once "PHPUnit/Framework/TestSuite.php";

require_once 'lib/RL/CyrDate.php';
require_once 'lib/RSSAggregator/RssTmpStore.php';
require_once 'lib/RSSAggregator/RssPublisher.php';
//require_once 'lib/RSSAggregator/RssDownload.php';
require_once 'lib/RSSAggregator/RssItem.php';
require_once 'utest/RssFakes.php';

class Model_AclAssistant {
	
}

class RssFeedList {
	}

class Model_Item {
	
}
	
class Model_RssFeedSnapshots {
	public $snapshots = array();
	public function getSnapshotsByFeedOid($feed_oid) { return $this->snapshots[$feed_oid]; }
	public function makeHash($a) { return md5($a); }
	public function rmSnapshots() {}
	public function addSnapshots() {}
	
	public function addFakeSnapshot($feed_oid, $hash) {
		$this->snapshots[$feed_oid][$hash] = 1;
	}
}

class RssPublisherTest extends PHPUnit_Framework_TestCase {
	
	public function testNewItems() {
		
		$result = $this->tmp_store->retAll();
		$this->assertEquals(count($result), 1);
		foreach($result as $feed_oid=>$items) {
			$new_items = $this->publisher->getNewItems($feed_oid, $items);
			$this->assertEquals(count($new_items), 132);
		}	
	}

    /**
     * Runs the test methods of this class.
     *
     * @access public
     * @static
     */
    public static function main() {
        require_once "PHPUnit/TextUI/TestRunner.php";

        $suite  = new PHPUnit_Framework_TestSuite("RssPublisherTest");
        $result = PHPUnit_TextUI_TestRunner::run($suite);
    }

    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     *
     * @access protected
     */
    protected function setUp() {
    	$dlq = new Model_RssDLQueue();
    	$lw = new RssLibWrapper();
    	$this->tmp_store = new RssTmpStore($dlq, $lw);
    	
    	// приготовим снэпщоты чтобы там было немного свежих
    	$this->rss_fs = new Model_RssFeedSnapshots();
    	$this->tmp_store->getAll();
    	foreach($this->tmp_store->retAll() as $feed_oid=>$items) {
    		$i = 0;
    		foreach ($items as $k=>$item) {
    			//var_dump($item); die();
    			$item_hash = $this->rss_fs->makeHash($item->link.$item->title);
    			$this->rss_fs->addFakeSnapshot($feed_oid, $item_hash);
    			$i++;
    			if ($i>4) break; // сделаем первые четыре итема фейками
    		}
    		
    		
    		break;
		}
		
    	$lw->resetCounter();
    	
    	$this->publisher = new RssPublisher($this->tmp_store, $this->rss_fs, NULL);
    }

    /**
     * Tears down the fixture, for example, close a network connection.
     * This method is called after a test is executed.
     *
     * @access protected
     */
    protected function tearDown() {
    	unset($this->tmp_store);
    	unset($this->publisher);
    }
}

if (PHPUnit_MAIN_METHOD == "RssPublisherTest::main") {
    RssPublisherTest::main();
}
?>
